from django.db import models
from rest_framework import serializers
from user.models import SysUser
from category.models import Category
from tag.models import Tag


class Article(models.Model):
    id = models.AutoField(primary_key=True)
    author = models.ForeignKey(SysUser, on_delete=models.CASCADE, default='1', verbose_name='作者')
    title = models.CharField(max_length=255, verbose_name='标题')
    cover_image = models.CharField(max_length=500, verbose_name='封面图')
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, verbose_name='分类')
    tags = models.ManyToManyField(Tag, verbose_name='标签')
    content = models.TextField(verbose_name='内容')
    status = models.IntegerField(default=0, verbose_name='状态')
    heat = models.IntegerField(default=0, verbose_name='热度')
    publish_time = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        db_table = 'sys_article'


class ArticleSerializer(serializers.ModelSerializer):
    author = serializers.PrimaryKeyRelatedField(queryset=SysUser.objects.all())
    category = serializers.PrimaryKeyRelatedField(queryset=Category.objects.all())
    tags = serializers.PrimaryKeyRelatedField(many=True, queryset=Tag.objects.all())

    class Meta:
        model = Article
        fields = '__all__'
